import { Controller, Get, Post, Body, Res, Session, Query } from '@nestjs/common'
import { CodeService } from './code.service'
import { Public } from '@/decorator/public.decorator'

@Controller('code')
export class CodeController {
  constructor(private readonly codeService: CodeService) {}

  @Post()
  @Public()
  create(@Body() body, @Session() session) {
    if (session.code.toLocaleLowerCase() === body?.code.toLocaleLowerCase()) {
      return { data: '', message: '验证码正确' }
    } else {
      return { data: '', message: '验证码错误' }
    }
  }

  @Get()
  @Public()
  findAll(@Query() query: { theme?: string }, @Res() res, @Session() session) {
    const captcha = this.codeService.createCode(query.theme)
    session.code = captcha.text
    res.type('image/svg+xml')
    res.send(captcha.data)
  }
}
